package com.prd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.mapper.EntNarrationRecordMapper;
import com.prd.model.EntNarrationRecord;
import com.prd.model.LoginUser;
import com.prd.service.EntNarrationRecordService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntNarrationRecordServiceImpl extends ServiceImpl<EntNarrationRecordMapper, EntNarrationRecord> implements EntNarrationRecordService {

    @Autowired
    private EntNarrationRecordMapper entNarrationRecordMapper;

    @Override
    public int insertInfo(EntNarrationRecord entNarrationRecord) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entNarrationRecord);
        entNarrationRecord.setStatus(CheckStatus.ONE.getCode());
        return entNarrationRecordMapper.insert(entNarrationRecord);
    }

    @Override
    public int updateInfo(EntNarrationRecord entNarrationRecord) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entNarrationRecord);
        return entNarrationRecordMapper.updateById(entNarrationRecord);
    }

    @Override
    public int checkRecord(Long recordId) {
        EntNarrationRecord entNarrationRecord = entNarrationRecordMapper.selectById(recordId);
        if (entNarrationRecord == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entNarrationRecord.setStatus(CheckStatus.TWO.getCode());
        return updateInfo(entNarrationRecord);
    }
}
